﻿using System;
using SmartSiteManagementSystem.IRepository.User;
using SmartSiteManagementSystem.Models.User;
using SmartSiteManagementSystem.Repository.DBHelper;
using System.Data;
using Dapper;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using MySql.Data.MySqlClient;

namespace SmartSiteManagementSystem.Repository.User
{
    public class UserHealthMonitorInfoRepository : IUserHealthMonitorInfoRepository
    //,IDisposable
    {
        #region dataTable
        /*
drop table if exists `user_health_monitor`;
create table `user_health_monitor`( 
	`log_id` int(11) not null AUTO_INCREMENT primary key, 
    `user_id` int NOT NULL default 0,
	`heart_beat` double(10,2)  NOT NULL default 0,
    `temperature`  double(10,2)  NOT NULL default 0,
    `status_type` int NOT NULL default 0,
    `collectc_time` datetime not null default now(),
	`create_time` datetime not null default now(),
	`update_time` datetime not null default now()
) default charset=utf8;
         */
        #endregion
        //private IDbConnection _readConn;
        //private IDbConnection _writeConn;

        public bool Delete(Hashtable param)
        {
            bool back = false;
            if (param == null)
                param = new Hashtable();
            String querySql = @"delete from user_health_monitor";
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where ");
                    int parCount = 0;
                    if (param.ContainsKey("user_id"))
                    {
                        pars.Add("user_id", param["user_id"]);

                        whereCondition.Append(" user_id =@user_id");
                        parCount++;
                    }
                    if (param.ContainsKey("log_id"))
                    {
                        pars.Add("log_id", param["log_id"]);

                        whereCondition.Append(" log_id =@log_id");
                        parCount++;
                    }
                }
                string execSql = querySql + whereCondition.ToString();
                int backInt = _writeConn.Execute(execSql, pars);
                if (backInt > 0)
                {
                    back = true;
                }
            }
            return back;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public UserHealthMonitorInfo Get(Hashtable param)
        {
            UserHealthMonitorInfo back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"SELECT log_id as LogId,user_id as UserId,heart_beat as HeartBeat,temperature as Temperature,status_type as  StatusType,collectc_time as CollectTime,create_time as  CreateTime, update_time as UpdateTime FROM user_health_monitor";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {

                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("log_id"))
                    {
                        pars.Add("log_id", param["log_id"]);
                        whereCondition.Append(" and log_id =@log_id");
                    }
                    if (param.ContainsKey("user_id"))
                    {
                        pars.Add("user_id", param["user_id"]);
                        whereCondition.Append(" and user_id =@user_id");
                    }
                    if (param.ContainsKey("status_type"))
                    {
                        pars.Add("status_type", param["status_type"]);
                        whereCondition.Append(" and status_type =@status_type");
                    }

                    if (param.ContainsKey("start_time"))
                    {
                        pars.Add("start_time", param["start_time"]);
                        whereCondition.Append(" and collectc_time >='@start_time'");
                    }

                    if (param.ContainsKey("end_time"))
                    {
                        pars.Add("end_time", param["end_time"]);
                        whereCondition.Append(" and collectc_time <='@end_time'");
                    }

                }
                whereCondition.Append(" limit 1");
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.QueryFirstOrDefault<UserHealthMonitorInfo>(execSql, pars);
            }
            return back;
        }

        public int GetCount(Hashtable param)
        {
            int back = 0;
            if (param == null)
                param = new Hashtable();
            String querySql = "select count(log_id) from user_health_monitor";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("log_id"))
                    {
                        pars.Add("log_id", param["log_id"]);
                        whereCondition.Append(" and log_id =@log_id");
                    }
                    if (param.ContainsKey("user_id"))
                    {
                        pars.Add("user_id", param["user_id"]);
                        whereCondition.Append(" and user_id =@user_id");
                    }
                    if (param.ContainsKey("status_type"))
                    {
                        pars.Add("status_type", param["status_type"]);
                        whereCondition.Append(" and status_type =@status_type");
                    }

                    if (param.ContainsKey("start_time"))
                    {
                        pars.Add("start_time", param["start_time"]);
                        whereCondition.Append(" and collectc_time >=@start_time");
                    }

                    if (param.ContainsKey("end_time"))
                    {
                        pars.Add("end_time", param["end_time"]);
                        whereCondition.Append(" and collectc_time <=@end_time");
                    }
                }

                string execSql = querySql + whereCondition.ToString();
                #endregion
                back = _readConn.QueryFirstOrDefault<int>(execSql, pars);
            }
            return back;
        }

        public IList<UserHealthMonitorInfo> GetList(Hashtable param, string orderBy, int first, int rows)
        {

            IList<UserHealthMonitorInfo> back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"SELECT log_id as LogId,user_id as UserId,heart_beat as HeartBeat,temperature as Temperature,status_type as  StatusType,collectc_time as CollectTime,create_time as  CreateTime, update_time as UpdateTime FROM user_health_monitor";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("log_id"))
                    {
                        pars.Add("log_id", param["log_id"]);
                        whereCondition.Append(" and log_id =@log_id");
                    }
                    if (param.ContainsKey("user_id"))
                    {
                        pars.Add("user_id", param["user_id"]);
                        whereCondition.Append(" and user_id =@user_id");
                    }
                    if (param.ContainsKey("status_type"))
                    {
                        pars.Add("status_type", param["status_type"]);
                        whereCondition.Append(" and status_type =@status_type");
                    }

                    if (param.ContainsKey("start_time"))
                    {
                        pars.Add("start_time", param["start_time"]);
                        whereCondition.Append(" and collectc_time >=@start_time");
                    }

                    if (param.ContainsKey("end_time"))
                    {
                        pars.Add("end_time", param["end_time"]);
                        whereCondition.Append(" and collectc_time <=@end_time");
                    }
                }
                if (string.IsNullOrEmpty(orderBy))
                {
                    whereCondition.Append(" order by log_id desc");
                }
                else
                {
                    whereCondition.Append(" order by @orderBy");
                    pars.Add("orderBy", orderBy);
                }
                whereCondition.Append(" limit @first,@rows");
                pars.Add("first", first);
                pars.Add("rows", rows);
                #endregion
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.Query<UserHealthMonitorInfo>(execSql, pars).AsList<UserHealthMonitorInfo>();
            }
            return back;
        }

        public int Insert(UserHealthMonitorInfo obj)
        {
            int back = 0;
            #region sql
            string sql = @"INSERT INTO user_health_monitor
(    user_id,
	heart_beat,
    temperature,
    status_type,
    collectc_time,
	create_time,
	update_time
)
VALUES
(
@user_id,
@heart_beat,
@temperature,
@status_type,
@collectc_time,
@create_time,
@update_time);select last_insert_id();";
            #endregion
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                back = _writeConn.QueryFirst<int>(sql, new
                {
                    user_id=obj.UserId,
                    heart_beat=obj.HeartBeat,
                    temperature=obj.Temperature,
                    status_type=obj.StatusType,
                    collectc_time=obj.CollectTime,
                    create_time=obj.CreateTime,
                    update_time=obj.UpdateTime
                });
            }
            return back;
        }

        public bool Update(UserHealthMonitorInfo obj)
        {
            bool back = false;
            #region sql
            string sql = @"update user_health_monitor
set
user_id=@user_id,
	heart_beat=@heart_beat,
    temperature=@temperature,
    status_type=@status_type,
    collectc_time=@collectc_time,
	create_time=@create_time,
	update_time=@update_time
WHERE log_id =@log_id;";
            #endregion
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                int backint = _writeConn.Execute(sql, new
                {
                    log_id = obj.LogId,
                    user_id = obj.UserId,
                    heart_beat = obj.HeartBeat,
                    temperature = obj.Temperature,
                    status_type = obj.StatusType,
                    collectc_time = obj.CollectTime,
                    create_time = obj.CreateTime,
                    update_time = obj.UpdateTime
                });
                if (backint > 0)
                {
                    back = true;
                }
            }
            return back;
        }
    }
}

